package com.apestech.framework.xml.plugin.modul;

import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.StringUtil;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2019-01-26-16:32
 */
public class Model extends StringUtil{
    public class ModelField {
        private String extend;
        private String id;
        private JSONArray fields=new JSONArray();

        public void setModel(JSONObject map) {
            if(map==null) return;
            this.extend = map.getString("extend");
            this.id = map.getString("id");
        }

        public void addField(JSONObject map){
            if(map==null) return;
            fields.add(map);
        }

        public String getExtend() {
            return extend;
        }

        public JSONArray getFields() {
            return fields;
        }

        public String getId() {
            return id;
        }
    }

    private JSONObject model=null;
    private JSONObject baseModel=new JSONObject();
    private JSONObject allField=new JSONObject();

    public void setModel(String id, JSONObject map){
        ModelField f=getField(id);
        if(map==null) map=new JSONObject();
        map.put("id", id);
        f.setModel(map);
        baseModel.put(id, f);
    }

    public void addField(String id, JSONObject map){
        ModelField f=getField(id);
        f.addField(map);
        String sid=id.substring(0, id.indexOf(".")+1)+map.getString("id");
        allField.put(sid, map);
        baseModel.put(id, f);
    }

    public JSONObject getAllField(String id){
        JSONObject f= (JSONObject) allField.get(id);
        if(f==null){
            id=id.substring(id.indexOf(".")+2, id.length());
            f= (JSONObject) allField.get(id);
        }
        return f;
    }

    private ModelField getField(String id){
        if(baseModel.get(id)==null) return new ModelField();
        else return (ModelField) baseModel.get(id);
    }

    private void setModel(){
        model=new JSONObject();
        ArrayList<String> klist=baseModel.getKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String k=klist.get(i);
            if(model.get(k)==null){
                ModelField f= (ModelField) baseModel.get(k);
                JSONArray list=f.getFields();
                JSONArray mlist;
                if(model.get(k)==null) mlist=new JSONArray();
                else mlist= (JSONArray) model.get(k);
                mlist.addAll(list);
                model.put(k, mlist);
                while(f.getExtend()!=null){
                    String sid=f.getExtend();
                    String oid=f.getId();
                    int pos=oid.indexOf(".");
                    if(pos>=0) sid=leftStr(oid,pos)+"."+sid;
                    f= (ModelField) baseModel.get(sid);
                    if(f==null) f= (ModelField) baseModel.get(f.getExtend());
                    list=f.getFields();
                    if(model.get(oid)==null) mlist=new JSONArray();
                    else mlist= (JSONArray) model.get(oid);
                    mlist.addAll(list);
                    model.put(oid, mlist);
                }
            }
        }
    }

    public JSONObject getModel() {
        if(model==null) setModel();
        return model;
    }

    public JSONArray getModelFields(String id){
        if(model==null) setModel();
        int pos=id.indexOf(".");
        String sid=id;
        if(pos>=0) sid=rightStr(id, id.length()-pos-1);
        if(model.get(sid)!=null) return (JSONArray) model.get(sid);
        if(model.get(id)==null) return null;
        return (JSONArray) model.get(id);
    }

    public void addList(String id, String extend){
        if(model.get(extend)==null) return;
        JSONArray list= (JSONArray) model.get(extend);
        JSONArray mlist;
        if(model.get(id)==null) mlist=new JSONArray();
        else mlist= (JSONArray) model.get(id);
        mlist.addAll(list);
        model.put(id, mlist);
    }

}
